package com.coinworld.utils;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.ssl.SSLContexts;

import javax.net.ssl.SSLContext;
import java.io.FileInputStream;
import java.security.KeyStore;

/**
 * ReadSSl
 *
 * @author
 * @date 2020/2/20
 * @description
 */
@SuppressWarnings("deprecation")
public class ReadSSl {

    private static ReadSSl readSSL = null;

    private ReadSSl() {

    }

    public static ReadSSl getInstance() {
        if (readSSL == null) {
            readSSL = new ReadSSl();
        }
        return readSSL;
    }

    /**
     *   读取 apiclient_cert.p12 证书
     *
     * @return
     * @throws Exception
     */
    public SSLConnectionSocketFactory readCustomSSL() throws Exception {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        FileInputStream instream = new FileInputStream("/Users/xiemengjie/Desktop/apiclient_cert.p12");
        try {
            keyStore.load(instream, WxPayConfig.WXPAY_PARTNER.toCharArray());
        } finally {
            instream.close();
        }
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, WxPayConfig.WXPAY_PARTNER.toCharArray()).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        return sslsf;
    }

}
